package com.iotproject.forumservice.service.impl;

import com.iotproject.commonutils.R;
import com.iotproject.forumservice.client.UcenterClient;
import com.iotproject.forumservice.entity.ForumComment;
import com.iotproject.forumservice.entity.ForumQuestion;
import com.iotproject.forumservice.entity.vo.ReplyInFirstLevelVo;
import com.iotproject.forumservice.entity.vo.ReplyQuestionVo;
import com.iotproject.forumservice.entity.vo.SecondLevelCommentsVo;
import com.iotproject.forumservice.mapper.ForumCommentMapper;
import com.iotproject.forumservice.service.ForumCommentService;
import com.iotproject.forumservice.service.ForumQuestionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 论坛评论回复 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-04-13
 */
@Service
public class ForumCommentServiceImpl extends ServiceImpl<ForumCommentMapper, ForumComment> implements ForumCommentService {

    @Autowired
    UcenterClient ucenterClient;

    @Autowired
    ForumQuestionService forumQuestionService;

    @Override
    public List<SecondLevelCommentsVo> getList(String questionId, String parentId, String userDesc) {
        QueryWrapper<ForumComment> forumCommentQueryWrapper = new QueryWrapper<>();
        forumCommentQueryWrapper.eq("question_id", questionId);
        forumCommentQueryWrapper.eq("parent_id", parentId);
        if ("true".equals(userDesc)) {
            forumCommentQueryWrapper.orderByDesc("gmt_modified");
        }
        List<ForumComment> firstLevelList = baseMapper.selectList(forumCommentQueryWrapper);
        List<SecondLevelCommentsVo> list = new ArrayList<>();
        for (int i = 0; i < firstLevelList.size(); i++) {
            ForumComment forumComment = firstLevelList.get(i);
            SecondLevelCommentsVo secondLevelCommentsVo = new SecondLevelCommentsVo();
            ReplyQuestionVo replyQuestionVo = new ReplyQuestionVo();
            BeanUtils.copyProperties(forumComment, replyQuestionVo);
            //操作者信息
            Map<String, String> creatorUserInfo = forumQuestionService.getUserInfo(forumComment.getCreatorId());
            replyQuestionVo.setCreatorNickname(creatorUserInfo.get("nickname"));
            replyQuestionVo.setCreatorAvatar(creatorUserInfo.get("avatar"));
            replyQuestionVo.setCreatorSign(creatorUserInfo.get("sign"));
            //接收者信息
            Map<String, String> recipientUserInfo = forumQuestionService.getUserInfo(forumComment.getRecipientId());
            replyQuestionVo.setRecipientNickname(recipientUserInfo.get("nickname"));
            replyQuestionVo.setRecipientAvatar(recipientUserInfo.get("avatar"));
            replyQuestionVo.setRecipientSign(recipientUserInfo.get("sign"));

            replyQuestionVo.setShow(false);
            secondLevelCommentsVo.setReplyQuestionVo(replyQuestionVo);

            List<ReplyQuestionVo> childrenList = new ArrayList<>();
            secondLevelCommentsVo.setChildrenList(childrenList);
            list.add(secondLevelCommentsVo);
        }

        return list;
    }

    @Override
    public R replyOperatorInFirstLeve(ReplyInFirstLevelVo replyInFirstLevelVo) {
        String questionId = replyInFirstLevelVo.getQuestionId();
        //查询问题是否存在
        ForumQuestion question = forumQuestionService.getById(questionId);
        if (question == null) {
            return R.error().message("当前话题不存在");
        }
        String creatorId = replyInFirstLevelVo.getCreatorId();
        String recipientId = replyInFirstLevelVo.getRecipientId();
        String parentId = replyInFirstLevelVo.getParentId();
        Integer type = replyInFirstLevelVo.getType();
        String description = replyInFirstLevelVo.getDescription();
        ForumComment forumComment = new ForumComment();
        forumComment.setQuestionId(questionId);
        forumComment.setParentId(parentId);
        forumComment.setType(type);
        forumComment.setCreatorId(creatorId);
        forumComment.setRecipientId(recipientId);
        forumComment.setDescription(description);
        int insert = baseMapper.insert(forumComment);
        if (insert == 0) {
            return R.error().message("回复失败");
        }
        //这里可以得到 forumComment 的id，增加一次话题回复数
        if ("0".equals(parentId)) {//一级评论
//            baseMapper.increaseCommentCount(forumComment.getId());
        } else {//二级评论
            baseMapper.increaseCommentCount(forumComment.getParentId());
        }
        //question表评论数 + 1
        forumQuestionService.increaseCommentCount(questionId);
        return R.ok();
    }

    @Override
    public R likeOperator(String forumCommentId) {
        //查询评论是否存在
        ForumComment forumComment = baseMapper.selectById(forumCommentId);
        if (forumComment == null) {
            return R.error().message("评论不存在");
        }
        baseMapper.increaseLikeCount(forumCommentId);
        //forum_question 表赞数 + 1
        forumQuestionService.increaseLikeCount(forumComment.getQuestionId());
        return R.ok();
    }

    @Override
    public R cancelLikeOperator(String forumCommentId) {
        //查询评论是否存在
        ForumComment forumComment = baseMapper.selectById(forumCommentId);
        if (forumComment == null) {
            return R.error().message("评论不存在");
        }
        baseMapper.decreaseLikeOperator(forumCommentId);
        //forum_question 表赞数 - 1
        forumQuestionService.decreaseLikeOperator(forumComment.getQuestionId());
        return R.ok();
    }
}
